namespace Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText
{
    using System.Drawing;
    using System.Text;

    public abstract class BaseText
    {
        private int _charsWidth;
        private int _charsHeight;

        /// <summary>
        /// total charactor count in Width.
        /// </summary>
        /// <remarks>
        /// can not use "public virtual int CharsWidth { get; set; }"
        /// because this property is virtual.
        /// </remarks>
        public virtual int CharsWidth
        {
            get { return _charsWidth; }
            set { _charsWidth = value; }
        }

        /// <summary>
        /// total charactor count in Height.
        /// </summary>
        /// <remarks>
        /// can not use "public virtual int CharsWidth { get; set; }"
        /// because this property is virtual.
        /// </remarks>
        public virtual int CharsHeight
        {
            get { return _charsHeight; }
            set { _charsHeight = value; }
        }

        /// <summary>
        /// original image
        /// </summary>
        public Photo Photo { get; set; }

        public BaseText(Photo photo, int charsWidth, int charsHeight)
        {
            Photo = photo;
            _charsWidth = charsWidth;
            _charsHeight = charsHeight;
        }

        public virtual string GenerateText()
        {
            StringBuilder sb = new StringBuilder();

            Photo thumbnail = Photo.GetThumbnailImage(CharsWidth, CharsHeight);

            for (int h = 0; h < CharsHeight; h++)
            {
                for (int w = 0; w < CharsWidth; w++)
                {
                    sb.Append(
                        ConvertPixelToString(thumbnail.GetPixel(w, h)));
                }

                sb.Append(NewLine());
            }
            return sb.ToString();
        }

        protected virtual string NewLine()
        {
            return System.Environment.NewLine;
        }

        protected abstract string ConvertPixelToString(Color color);
    }
}